﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcBlog.Common.Models
{
    public class UserDataAccess
    {

        private MvcBlogEntities db;

        public UserDataAccess(Object db_access)
        {
            db = (MvcBlogEntities)db_access;
        }


        public IQueryable<User> getUsers()
        {
            try
            {
                var res = db.User; //OrderByDescending(q => q.CreateDate).Skip(skip).Take(take)
                /*var query = from s in db.User
                               where s.Username
                               orderby s
                               select s;*/
                

                return res;
            }
            catch
            {
                return null;
            }
        }

        public User getUserByID(int user_id)
        {
            try
            {
                var res = db.User.Where(u => u.User_ID == user_id);
                return res.First();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public User updateUserByID(User updatedUser)
        {

            try
            {
                User originalUser = db.User.Single(us => us.User_ID == updatedUser.User_ID);
                db.User.ApplyCurrentValues(updatedUser);
                db.SaveChanges();

                return updatedUser;
            }
            catch (Exception)
            {
                return null;
            }

        }



    }
}
